package com.xiaotao.controller;

import com.xiaotao.entity.Merchant;
import com.xiaotao.service.MerchantService;
import com.xiaotao.util.ResponseUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

@Api(tags = "商户相关")
@RestController
@RequestMapping("merchant")
public class MerchantController {
    @Resource
    private MerchantService merchantService;

    //添加商户信息
    @ApiOperation(value = "添加商户")
    @PostMapping("addMerchant")
    public ResponseUtil Add(@RequestBody Merchant merchant) {
        return merchantService.save(merchant);
    }

    //商户的登录
    @ApiOperation(value = "商户登录")
    @PostMapping("login")
    public ResponseUtil login(@RequestBody Merchant merchant) {
        return merchantService.login(merchant);
    }

    //查询所有商户
    @ApiOperation(value = "查询所有商户")
    @GetMapping("{pageNum}/{pageSize}")
    public ResponseUtil findAll(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        return merchantService.findAll(pageNum, pageSize);
    }
    //查询所有商户
    @ApiOperation(value = "查询所有商户")
    @GetMapping()
    public ResponseUtil findAll() {
        return merchantService.findAll2();
    }

    //删除商户
    @ApiOperation(value = "删除商户")
    @DeleteMapping("{id}")
    public ResponseUtil delete(@PathVariable Integer id) {
        return merchantService.delete(id);
    }

    //根据id查询商户
    @ApiOperation(value = "根据id查询商户")
    @GetMapping("/{id}")
    public ResponseUtil findById(@PathVariable Integer id) {
        return merchantService.findById(id);
    }

    //商户上传图片
    @ApiOperation(value = "商户图片上传")
    @PostMapping
    public ResponseUtil uploadImage(MultipartFile file) throws Exception {
        return merchantService.uploadImage(file, "merchant");
    }

    //条件查询
    @ApiOperation(value = "关键字查询")
    @GetMapping("search/{pageNum}/{pageSize}")
    public ResponseUtil searchByKeyWord(@RequestParam(required = false) String keyword,
                                        @RequestParam(required = false) Integer status, @PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        return merchantService.searchByKeyWord(keyword, status, pageNum, pageSize);
    }
    //更新商户信息
    @ApiOperation(value = "更新商户信息")
    @PutMapping
    public ResponseUtil update(@RequestBody Merchant merchant){
        return merchantService.update(merchant);
    }
}
